package org.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.demo.constant.MessageConstant;
import org.example.demo.entity.IntroducerUser;
import org.example.demo.service.IntroducerUserService;
import org.example.demo.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 介绍人用户关联控制器
 */
@RestController
@RequestMapping("introducerUser")
public class IntroducerUserController {

    @Resource
    private IntroducerUserService introducerUserService;

    /**
     * 分页查询介绍人用户关联
     * 
     * @param introducerUser 查询条件
     * @param pageNum        页码
     * @param pageSize       每页大小
     * @return 查询结果
     */
    @PostMapping("page")
    public AjaxResult page(@RequestBody IntroducerUser introducerUser,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<IntroducerUser> page = introducerUserService.selectPage(introducerUser, pageNum, pageSize);

        Map<String, Object> result = new HashMap<>();
        result.put("page", page);
        result.put("total", page.getTotal());

        return AjaxResult.success(result);
    }

    /**
     * 根据ID查询介绍人用户关联
     * 
     * @param id 关联ID
     * @return 查询结果
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        IntroducerUser introducerUser = introducerUserService.getById(id);

        if (introducerUser != null) {
            return AjaxResult.success(introducerUser);
        } else {
            return AjaxResult.error("介绍人用户关联不存在");
        }
    }

    /**
     * 新增介绍人用户关联
     * 
     * @param introducerUser 介绍人用户关联信息
     * @return 操作结果
     */
    @PostMapping
    public AjaxResult add(@RequestBody IntroducerUser introducerUser) {
        if (introducerUserService.save(introducerUser)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 修改介绍人用户关联
     * 
     * @param introducerUser 介绍人用户关联信息
     * @return 操作结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody IntroducerUser introducerUser) {
        if (introducerUserService.updateById(introducerUser)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除介绍人用户关联
     * 
     * @param id 关联ID
     * @return 操作结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        if (introducerUserService.removeById(id)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 根据介绍人ID查询关联的用户ID列表
     *
     * @param introducerId 介绍人ID
     * @return 用户ID列表
     */
    @GetMapping("userIds/{introducerId}")
    public AjaxResult getUserIdsByIntroducerId(@PathVariable Long introducerId) {
        List<Long> userIds = introducerUserService.getUserIdsByIntroducerId(introducerId);
        return AjaxResult.success(userIds);
    }

    /**
     * 根据用户ID查询关联的介绍人ID列表
     *
     * @param userId 用户ID
     * @return 介绍人ID列表
     */
    @GetMapping("introducerIds/{userId}")
    public AjaxResult getIntroducerIdsByUserId(@PathVariable Long userId) {
        List<Long> introducerIds = introducerUserService.getIntroducerIdsByUserId(userId);
        return AjaxResult.success(introducerIds);
    }

    /**
     * 批量保存介绍人与用户的关联关系
     *
     * @param introducerId 介绍人ID
     * @param userIds      用户ID列表
     * @return 操作结果
     */
    @PostMapping("batch/{introducerId}")
    public AjaxResult saveBatch(@PathVariable Long introducerId, @RequestBody List<Long> userIds) {
        if (introducerUserService.saveBatch(introducerId, userIds)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 删除介绍人与用户的关联关系
     *
     * @param introducerId 介绍人ID
     * @param userId       用户ID
     * @return 操作结果
     */
    @DeleteMapping("{introducerId}/{userId}")
    public AjaxResult remove(@PathVariable Long introducerId, @PathVariable Long userId) {
        if (introducerUserService.remove(introducerId, userId)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error(MessageConstant.INTERNAL_SERVER_ERROR);
        }
    }
}